<template>
    <div class="user">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="table" style="margin-top: 15px;">
            <template>
                <div>
                    <el-input placeholder="请输入内容" v-model="searchValue" class="input-with-select" style="width:300px" @keydown.native.enter="searchUserList">
                        <el-button slot="append" icon="el-icon-search" @click="searchUserList"></el-button>
                    </el-input>
                    <el-button type="success" plain @click="showAddDialog">添加用户</el-button>
                </div>
                <el-table
                    :data="userList"
                    border
                    style="width: 100%;margin-top:20px">
                    <el-table-column
                    type="index"
                    width="50">
                    </el-table-column>
                    <el-table-column
                    prop="username"
                    label="姓名"
                    width="180">
                    </el-table-column>
                    <el-table-column
                    prop="email"
                    label="邮箱"
                    width="180">
                    </el-table-column>
                    <el-table-column
                    prop="mobile"
                    label="电话">
                    </el-table-column>
                    <el-table-column label="状态"  width="100" align="center">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.mg_state"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    @change="changeUserState(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" style="width:80px" @click="showEditDialog(scope.row)">编辑</el-button>
                    <el-button type="primary" icon="el-icon-delete" style="width:80px" @click="deleteUserSubmit(scope.row.id)">删除</el-button>
                    <el-button type="primary" style="width:80px" @click="showGrantDialog(scope.row)">授权角色</el-button>
                    </template>
                    </el-table-column>
                </el-table>
                 <el-pagination
                 class="user-page"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="1"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
             </el-pagination>
            </template>
        </div>
        <!-- 添加用户对话框 -->
        <el-dialog title="添加用户" :visible.sync="addDialogFormVisible">
            <el-form :model="addform" :rules="rules" ref="addform">
                <el-form-item label="用户名" prop="username">
                <el-input v-model="addform.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                <el-input v-model="addform.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
                <el-input v-model="addform.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
                <el-input v-model="addform.mobile" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUserSubmit('addform')">确 定</el-button>
            </div>
            <!-- 编辑用户对话框 -->
        </el-dialog>
        <!-- 编辑用户对话框 -->
        <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
            <el-form :model="editform" :rules="rules" ref="editform">
                <el-form-item label="用户名" prop="username" disabled>
                <el-input v-model="editform.username" auto-complete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
                <el-input v-model="editform.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
                <el-input v-model="editform.mobile" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUserSubmit('editform')">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 角色授权 -->
        <el-dialog title="授权角色" :visible.sync="grantDialogFormVisible">
          <el-form :model="grantform" ref="grantform">
            <el-form-item label="用户名" :label-width="formLabelWidth">
              <el-input v-model="grantform.username" auto-complete="off" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="角色分配" :label-width="formLabelWidth">
              <el-select v-model="grantform.rid" placeholder="请选择">
                  <el-option
                    v-for="item in grantList"
                    :key="item.id"
                    :label="item.roleName"
                    :value="item.id">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="grantDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="grantUserSubmit">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>
<script>
import {getUserList, addUser, editUser, deleteUser, getRolesList, grantUser, changeuserstate} from '@/api/index.js'
export default {
  data () {
    return {
      value2: '',
      userList: [],
      currentpage: 1,
      pagesize: 10,
      pagenum: 1,
      total: 0,
      grantList: [],
      mg_state: false,
      searchValue: '',
      // addDialogFormVisible: false,
      addform: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      editform: {
        name: '',
        password: '',
        email: '',
        mobile: '',
        id: ''
      },
      grantform: {
        name: '',
        rid: '',
        id: ''
      },
      formLabelWidth: '100',
      addDialogFormVisible: false,
      editDialogFormVisible: false,
      grantDialogFormVisible: false,
      // 添加用户的表单验证
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
        ],
        mobile: [
          { required: true, message: '电话不能为空' }
        ]
      }
    }
  },
  methods: {
    // 1 分页事件:当每页显示的记录数设置变化的时候触发 ==>改变每页显示的条数
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initList()
    },
    // 2 分页事件:当当前页码发生变化的时候触发
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.initList()
    },
    // 3 实现用户数据的搜索
    searchUserList () {
      this.initList()
    },
    // 4 显示添加对话框
    showAddDialog () {
      this.addDialogFormVisible = true
    },
    // 5 实现用户添加操作
    addUserSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 实现数据新增
          addUser(this.addform)
            .then((res) => {
              if (res.meta.status === 201) {
                this.$message({
                  type: 'success',
                  message: res.meta.msg
                })
                this.initList()
                this.addDialogFormVisible = false
                // console.log(this.addDialogFormVisible)
              } else {
                this.$message({
                  type: 'success',
                  message: res.meta.msg
                })
              }
            })
        } else {
          this.$message({
            type: 'warning',
            message: '数据输入不合法'
          })
        }
      })
    },
    // 6 打开用户编辑对话框
    showEditDialog (row) {
      // console.log(row)
      this.editform.username = row.username
      this.editform.email = row.email
      this.editform.mobile = row.mobile
      this.editform.id = row.id
      this.editDialogFormVisible = true
    },
    // 7 实现用户编辑操作
    editUserSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 实现数据编辑
          editUser(this.editform)
            .then((res) => {
              if (res.meta.status === 200) {
                this.$message({
                  type: 'success',
                  message: res.meta.msg
                })
                this.editDialogFormVisible = false
                // console.log(123)
                this.initList()
                // console.log(this.editDialogFormVisible)
              } else {
                this.$message({
                  type: 'success',
                  message: res.meta.msg
                })
              }
            })
        } else {
          this.$message({
            type: 'warning',
            message: '数据输入不合法'
          })
        }
      })
    },
    // 8 实现删除用户操作
    deleteUserSubmit (id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then((res) => {
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
            this.initList()
          } else {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 9 打开授权对话框
    showGrantDialog (row) {
      getRolesList().then((res) => {
        // console.log(res)
        this.grantList = res.data
      })
      this.grantDialogFormVisible = true
      this.grantform.username = row.username
      this.grantform.id = row.id
    },
    // 10 实现授权提交
    grantUserSubmit () {
      // console.log(this.grantform)
      grantUser(this.grantform)
        .then((res) => {
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
            this.grantDialogFormVisible = false
            // console.log(123)
            this.initList()
            // console.log(this.editDialogFormVisible)
          } else {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
          }
        })
    },
    // 11 修改用户状态
    changeUserState (row) {
      // console.log(row)
      changeuserstate({uid: row.id, type: row.mg_state})
        .then((res) => {
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
            this.initList()
          } else {
            this.$message({
              type: 'success',
              message: res.meta.msg
            })
          }
        })
    },
    // 加载用户列表数据
    initList () {
      getUserList({params: {query: this.searchValue, pagenum: this.pagenum, pagesize: this.pagesize}})
        .then((res) => {
          // console.log(res)
          this.userList = res.data.users
          this.total = res.data.total
        })
    }
  },
  mounted () {
    this.initList()
  }
}
</script>
<style lang="scss" scoped>
.user {
  .search-input {
    width: 300px;
  }
  .user-page{
      background-color: rgb(214, 210, 210);
      margin-top: 10px;
  }
  /* 覆盖element导航菜单的样式 */
.el-breadcrumb {
  background-color: #F5F5F5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
}
</style>
